package com.car.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.dto.SysUserDto;
import com.car.entity.SysUser;
import com.car.mapper.UserMapper;
import com.car.result.Result;
import com.car.service.UserService;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser>
        implements UserService {

    @Resource
    private UserMapper userMapper;


    /**
     * 用户登录
     */
    @Override
    public Result<?> userLogin(SysUser sysUser) {

        if (StringUtils.isNullOrEmpty(sysUser.getUsername()) || StringUtils.isNullOrEmpty(sysUser.getPassword())) {
            return Result.fail("字段不可空");
        }

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, sysUser.getUsername());
        SysUser user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            return Result.fail("未找到该用户");
        }

        if (SecureUtil.md5(sysUser.getPassword()).equals(user.getPassword())) {
            StpUtil.login(user.getId());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return Result.ok(tokenInfo);
        } else {
            return Result.fail("用户名或密码错误");
        }
    }

    /**
     * 用户注册
     */
    @Override
    public Result<?> userRegister(SysUser sysUser) {
        if (StringUtils.isNullOrEmpty(sysUser.getUsername()) || StringUtils.isNullOrEmpty(sysUser.getPassword())) {
            return Result.fail("字段不可空");
        }

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, sysUser.getUsername());
        SysUser user = userMapper.selectOne(queryWrapper);
        if (!Objects.isNull(user)) {
            return Result.fail("用户名已被注册");
        }

        if ("admin".equals(sysUser.getUsername())) {
            sysUser.setRole("admin");
        } else {
            sysUser.setRole("user");
        }

        sysUser.setPassword(SecureUtil.md5(sysUser.getPassword()));

        int insert = userMapper.insert(sysUser);
        return insert > 0 ? Result.ok("注册成功") : Result.ok("注册失败");
    }

    /**
     * 更新用户密码
     */
    @Override
    public Result<?> updatePassword(SysUserDto sysUserDTO) {
        if (Objects.isNull(sysUserDTO.getUserId())) {
            return Result.fail("用户ID为空");
        }
        SysUser user = userMapper.selectById(sysUserDTO.getUserId());
        if (Objects.isNull(user)) {
            return Result.fail("未找到该用户");
        }

        if (!user.getPassword().equals(SecureUtil.md5(sysUserDTO.getOldPassword()))) {
            return Result.fail("旧密码错误");
        }

        SysUser updateUser = new SysUser(sysUserDTO.getUserId(), sysUserDTO.getUsername(),
                SecureUtil.md5(sysUserDTO.getNewPassword()), null, new Date());

        int i = userMapper.updateById(updateUser);


        return i == 1 ? Result.ok("更新成功") : Result.fail("更新失败");
    }

}
